<script>
  import { useChatStore } from '@/store/chat';
  import { useUserStore } from '@/store/user';
  import { useCheckAppUpdate } from '@/hooks/useCheckAppUpdate';

  export default {
    onLaunch: function () {
      console.log('App Launch');
    },
    onShow: function () {
      console.log('App Show');
      const userStore = useUserStore();
      const chatStore = useChatStore();
      // #ifdef APP-PLUS
      const args = plus.runtime.arguments;
      const url = JSON.stringify(args);
      // 存储url
      uni.setStorageSync('urlStorage', url);

      // 手动解析参数
      const params = {};
      const queryString = url.split('?')[1];
      if (queryString) {
        const keyValuePairs = queryString.split('&');
        keyValuePairs.forEach(pair => {
          const [key, value] = pair.split('=');
          params[key] = decodeURIComponent(value || '');
        });
      }
      const token = params.token;
      const urlStorageTokenVal = uni.getStorageSync('urlStorageToken');
      if (token && urlStorageTokenVal != token) {
        userStore.setToken(token);
        uni.setStorageSync('urlStorageToken', token);
        userStore.getUserInfo();
        // uni.switchTab({
        //   url: '/pages/chatList/index'
        // });
        uni.navigateTo({
          url: `/pages/chat/index?agentId=${chatStore.agentId}`
        });
      }

      useCheckAppUpdate();
      // #endif
    },
    onHide: function () {
      console.log('App Hide');
    }
  };
</script>

<style lang="scss">
  /*每个页面公共css */
  .px-14 {
    padding: 0 14rpx * 2;
  }

  .spinner-2 {
    animation: spin 2s linear infinite;
  }

  .spinner-1 {
    animation: spin 1s linear infinite;
  }

  @keyframes spin {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(360deg);
    }
  }
</style>
